package com.tyk.bookstore.front.payment.controller;


import com.tyk.bookstore.front.common.core.model.result.R;
import com.tyk.bookstore.front.payment.model.query.MemberRechargeQuery;
import com.tyk.bookstore.front.payment.model.query.OrderPayQuery;
import com.tyk.bookstore.front.payment.model.vo.MemberRechargeVo;
import com.tyk.bookstore.front.payment.service.PaymentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author tyk
 * @description 支付管理
 */
@RequestMapping({"api/payment"})
@Tag(name = "支付管理", description = "支付管理")
@RestController
@RequiredArgsConstructor
public class PaymentController {

    private static final Logger log = LoggerFactory.getLogger(PaymentController.class);
    private final PaymentService paymentService;

    @PostMapping({"order/pay"})
    @Operation(summary = "支付订单", description = "支付订单")
    public R<Object> payOrder(
            @Validated @RequestBody OrderPayQuery query
    ) {
        return R.ok(paymentService.payOrder(query));
    }

    @PostMapping({"member/recharge"})
    @Operation(summary = "会员充值", description = "会员充值")
    public R<MemberRechargeVo> recharge(@Validated @RequestBody MemberRechargeQuery query) {
        return R.ok(paymentService.recharge(query));
    }

    @PostMapping({"order/pay/ali/notify"})
    @Operation(summary = "支付宝支付成功的回调", description = "支付宝支付成功的回调")
    public R<Object> paySuccessAliCallback(HttpServletRequest req) {
        HashMap<String, String> map = new HashMap<>();
        req.getParameterMap().keySet().forEach(k -> map.put(k, req.getParameter(k)));
        paymentService.paySuccessAliCallback(map);
        return R.ok();
    }
}
